imports('../../../style/recycle');

var layer = require('{pub}/layer');
var tpl = require('../../views/list').content;
var template = require('template');
var datagrid = require('{com}/datagrid');
var request = require('request');

return function(e, elem){
    var _that = this;
    var _layer = layer({
        content:template.render('<div style="padding-top:50px;">'+tpl+'</div>', {
            title:'回收站',
            conid:'recyclebox'
        }),
        skin:'recycle',
        title:null,
        height:'100%',
        container:'#container',
        width:800,
        isClickMask:true,
        position:{
            right:'self*-1',
            top:0
        },
        cancel:{
            enable:false
        },
        button:[{
            id:'close',
            text:'<i class="iconfont">&#xe63c;</i>',
            name:'recycle'
        }],
        onInit:function(self){
            var that = this;
            self.isReload = false;
            self.main.height('100%');
            this.onResize(self);
            self.element.animate({'left':self.data.left-self.data.outerWidth}, 'fast', function(){
                that.position.right = 0;
            })
            
            that.datagrid = datagrid({
                layer:self,
                container:'#recyclebox',
                isBorder:false,
                fields:['id', 'name'],
                dataField:'data.accountList',
                paging:{
                    url:'account/getRecycleList',
                    isPage:false,
                    loading:null
                },
                events:{
                    'click .j-recover':'getData recover',
                    'click .j-destroy':'getData destroy'
                },
                columns:[{
                    title:'账套名称',
                    field:'name',
                    width:'200',
                    align:'left',
                    nowrap:true
                }, {
                    title:'建账日期',
                    field:'createDate',
                    width:'120',
                    nowrap:true
                }, {
                    title:'记账状态',
                    field:'accountStatus',
                    width:'120',
                    nowrap:true
                }, {
                    title:'凭证总数',
                    field:'voucherCount',
                    width:'100',
                    nowrap:true
                }, {
                    title:'操作',
                    content:renders({
                        <a class="f-cblue e-mr20 f-wsn j-recover"><i class="iconfont e-mr5">&#xe63b;</i>恢复</a>
                        <span class="f-dib j-destroy">
                            <a class="f-cblue f-wsn"><i class="iconfont e-mr5">&#xe63d;</i>彻底删除</a>
                        </span>
                    })
                }],
                getData:require('{pub}/function').getData,
                //恢复账套
                recover:function(e, elem, data){
                    layer.die(e, elem, data,
                        renders({
                            <dl>
                                <dt>确定要恢复以下账套？</dt>
                                <dd>“<%name%>”</dd>
                            </dl>
                        }),
                        function(self){
                            request.del('account/recover', data, {
                                200:function(){
                                    that.datagrid.paging.query('reload');
                                    _layer.isReload = true;
                                }
                            }, _layer)
                        }
                    )
                },
                //彻底删除账套
                destroy:require('./delete')
            })
        },
        onResize:function(self){
            self.element.css('top', 50);
            self.element.css('height', '-=50px');
            self._body.css('height', '-=50px');
        },
        onBeforeDestroy:function(self){
            if(!self.isClose){
                self.element.animate({'left':self.data.left+self.data.outerWidth}, 'fast', function(){
                    self.isClose = true;
                    if(self.isReload){
                        _that.paging.query('reload');
                    }
                    self.destroy();
                })
                return false
            }
        }
    })
}